<?php

namespace app\admin\controller;

use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\facade\Cache;
use app\admin\model\Config as ConfigModel;
use app\admin\model\MessageTpl;

class Config extends Common
{
    /**
     * 全局设置
     * @author admin
     * @date 2019-08-11
     */
    public function globalConfig()
    {
        try {
            if (Request::isPost()) {
                $data = input("post.");
                if (isset($data['return_money'])){
                    $password = $data['password'];
                    $pwd = Db::name('auth_admin')->where('id',session(config("auth.USER_AUTH_KEY")))->value('u_pwd');
                    if (md5($password) != $pwd){
                        return ['code' => 2000, 'msg' => '密码错误!'];
                    }
                }
                $type = input("get.type");
                $type = $type ? $type : 'SYSTEM_CONFIG';
                $result = ConfigModel::setConfig($type, json_encode($data));
                if ($result) {
                    Cache::delete('WEBSITE');
                    return ['code' => 0, 'msg' => '操作成功!'];
                }
                return ['code' => 2000, 'msg' => '操作失败!'];
            } else {
                $info = ConfigModel::getCfgByModule("SYSTEM_CONFIG");
                $seo = ConfigModel::getCfgByModule("SITE_SEO");
                $upload_img = ConfigModel::getCfgByModule("UPLOAD_IMG");
                $info = array_merge($info, $seo, is_array($upload_img) ? $upload_img : array());
                $this->assign('info', json_encode($info));
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    public function alisms(){
        try {
            if (Request::isPost()) {
                $data = input("post.");
                $result = ConfigModel::setConfig('ALISMS_CONFIG', json_encode($data));
                if ($result) {
                    return ['code' => 0, 'msg' => '操作成功!'];
                }
                return ['code' => 2000, 'msg' => '操作失败!'];
            } else {
                $info = ConfigModel::getCfgByModule("ALISMS_CONFIG");
                $this->assign('info', json_encode($info, true));
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 微信配置
     * @return array|string
     */
    public function wechat()
    {
        try {
            if (Request::isPost()) {
                $data = input("post.");
                $result = ConfigModel::setConfig('WECHAT_CONFIG', json_encode($data));
                if ($result) {
                    return ['code' => 0, 'msg' => '操作成功!'];
                }
                return ['code' => 2000, 'msg' => '操作失败!'];
            } else {
                $info = ConfigModel::getCfgByModule("WECHAT_CONFIG");
                $this->assign('info', json_encode($info, true));
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 发送邮件设置
     * @author admin
     * @date 2019-08-11
     */
    public function email()
    {
        try {
            if (Request::isPost()) {
                $data = input("post.");
                $result = ConfigModel::setConfig('SMTP_CONFIG', json_encode($data));
                if ($result) {
                    return ['code' => 0, 'msg' => '操作成功!'];
                }
                return ['code' => 2000, 'msg' => '操作失败!'];
            } else {
                $info = ConfigModel::getCfgByModule("SMTP_CONFIG");
                $this->assign('info', json_encode($info, true));
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 邮件模板列表
     * @author admin
     * @date 2018-04-05
     */
    public function template_message()
    {
        try {
            if (Request::isPost()) {
                $key = input('post.key');
                $page = input('page') ? input('page') : 1;
                $pageSize = input('limit') ? input('limit') : config('pageSize');
                $list = Db::name('template_message')
                    ->where('title', 'like', "%" . $key . "%")
                    ->order('id desc')
                    ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                    ->toArray();
                return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
            }
            return View::fetch('template_message_edit');
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 添加邮件模板
     * @author admin
     * @date 2018-04-05
     */
    public function template_message_add()
    {
        try {
            if ($_POST) {
                $MessageTpl = new MessageTpl();
                $data = input("request.");
                $id = input("request.id", 0, "intval");
                $result = $MessageTpl->templatesSave($id, $data);
                if ($result) {
                    return $result = ['code' => 0, 'msg' => '操作成功!'];
                }
                return $result = ['code' => 1, 'msg' => '操作失败!'];
            }
            return view("template_message_edit");
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编缉邮件模板
     * @author admin
     * @date 2018-04-05
     */
    public function template_message_edit()
    {
        try {
            if ($_POST) {
                $MessageTpl = new MessageTpl();
                $data = input("request.");
                $id = input("request.id", 0, "intval");
                $result = $MessageTpl->templatesSave($id, $data);
                if ($result) {
                    return $result = ['code' => 0, 'msg' => '操作成功!'];
                }
                return $result = ['code' => 1, 'msg' => '操作失败!'];
            }

            $id = input('request.id', 0, 'intval');
            if (!$id) {
                return $result = ['code' => 1, 'msg' => '参数错误!'];
            }
            $vo = Db::name('template_message')->find($id);
            $this->assign("data", $vo);
            return view("template_message_edit");
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 测试发送邮件
     * @author admin
     * @date 2019-08-11
     */

    public function trySend()
    {
        try {
            $sender = input('test_eamil');
            $content = input('test_eamil_info');
            //检查是否邮箱格式
            if (!is_email($sender)) {
                return ['code' => 1, 'msg' => '测试邮箱码格式有误'];
            }
            $send = send_email($sender, '测试邮件', $content);
            if ($send) {
                return ['code' => 0, 'msg' => '邮件发送成功！'];
            } else {
                return ['code' => 1, 'msg' => '邮件发送失败！'];
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }

}
